package java21demo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * java21的有序集合
 */
public class SequenceCollectionDemo {

    public static void main(String[] args) {
        List<String> s1 = List.of("S1", "s2"); // 这里生成的List是java.util.ImmutableCollections 不可变集合的实例
        System.out.println(s1.getClass());
        //s1.add("S3"); java.lang.UnsupportedOperationException
        List<Integer> list = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
        System.out.println("First element: " + list.getFirst());
        System.out.println("Last element: " + list.getLast());

        list.addFirst(0);
        list.addLast(11);
        System.out.println("在头部插入元素，在尾部插入元素之后: "+list);

        list.removeFirst();
        list.removeLast();
        System.out.println("在头部移除元素，在尾部移除元素之后: "+list);

        // A sequenced set is a Set that is a SequencedCollection that contains no duplicate elements.
        //     SequencedSet<E> reversed();    // covariant override
        LinkedHashSet<Integer> integerSet = new LinkedHashSet<>(list);
        System.out.println("integerSet: " + integerSet.reversed()); // [9, 8, 7, 6, 5, 4, 3, 2, 1]

        LinkedHashMap<String, Integer> sequenceMap = new LinkedHashMap<>();
        sequenceMap.put("one", 1);
        sequenceMap.put("two", 2);
        sequenceMap.put("three", 3);

        System.out.println("sequenceMap: " + sequenceMap);
        System.out.println("SeqMap First:" + sequenceMap.firstEntry());
        System.out.println("SeqMap Last:" + sequenceMap.lastEntry());

        System.out.println("SeqMap Poll First:" + sequenceMap.pollFirstEntry());
        System.out.println("SeqMap Poll Last:" + sequenceMap.pollLastEntry());
        System.out.println("SeqMap 剩余元素: " + sequenceMap);
    }
}
